package com.rxt.thermopro.viewmodel

import com.app.common.ex.logD
import com.app.common.viewmodel.BaseViewModel
import com.app.common.viewmodel.EventLiveData
import com.rxt.ble.BleManager
import com.rxt.ble.callback.BleWriteCallback
import com.rxt.ble.exception.BleException
import com.rxt.ble.utils.HexUtil
import com.rxt.thermopro.app.AppContext
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.EventAlarmTextData
import com.rxt.thermopro.bean.ProbeBean
import com.rxt.thermopro.bean.ProfileBean

class TP960ViewModel : BaseViewModel() {

    companion object {
        const val BLUETOOTH_SERVICE_UUID_960 = "93bb7cab-eb14-c5a9-2354-a24f4df9330f"
        const val BLUETOOTH_NOTIFY_UUID_960 = "0000ff01-0000-1000-8000-00805f9b34fb"
        const val BLUETOOTH_WRITE_UUID_960 = "0000ff02-0000-1000-8000-00805f9b34fb"
    }

    val alarmClockText1: EventLiveData<HashMap<Long, EventAlarmTextData>> = EventLiveData()
    val alarmClockText2: EventLiveData<HashMap<Long, EventAlarmTextData>> = EventLiveData()
    val alarmClockText3: EventLiveData<HashMap<Long, EventAlarmTextData>> = EventLiveData()
    val alarmClockText4: EventLiveData<HashMap<Long, EventAlarmTextData>> = EventLiveData()

    val getHistoryTempByCache: EventLiveData<Long> = EventLiveData()

    val compute2TempTime:EventLiveData<HashMap<String, String>> = EventLiveData()


    val notifyTP960ProbeStatus: EventLiveData<HashMap<String, Pair<Int, Int>>> = EventLiveData()

    fun setProfile(profileBean: ProfileBean, probeIndex: Int, address: String) {
        val num = (profileBean.highTemperatureNum.toIntOrNull() ?: 0) + 30
        var numStr = Integer.toHexString(num)
        numStr = when (numStr.length) {
            1 -> "0${numStr}00"
            2 -> "${numStr}00"
            3 -> "0${numStr.substring(2, 3)}${numStr.substring(0, 2)}"
            4 -> "${numStr.substring(2, 4)}${numStr.substring(0, 2)}"
            else -> numStr
        }
        val isOpen = if (num == 30) "00" else "01"
        val msg = "040${probeIndex}$isOpen$numStr"
        println("setProfile msg====$msg")
        val deviceList = BleManager.getInstance().allConnectedDevice.filter { it.mac == address }
        if (deviceList.isNotEmpty()) {
            BleManager.getInstance().write(
                deviceList[0],
                BLUETOOTH_SERVICE_UUID_960,
                BLUETOOTH_WRITE_UUID_960,
                HexUtil.hexStringToBytes(msg),
                object : BleWriteCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray
                    ) {
                        // 发送数据到设备成功
                        logD(" 蓝牙发送数据成功 setProfile  $msg")

                    }

                    override fun onWriteFailure(exception: BleException) {
                        logD(" 蓝牙发送数据失败  ${exception.toString()}")
                        // 发送数据到设备失败

                    }
                })
        }
    }


    /**
     * 获取历史数据
     */
    fun getHistoryTemp(address: String, probeIndex: Int) {
        val msg = "020$probeIndex"
        println("getHistoryTemp msg====$msg")
        if (!LocalDataDao.INSTANCE.needGetLastHistory(address)) {
            getHistoryTempByCache.value = System.currentTimeMillis()
            return
        }
        LocalDataDao.INSTANCE.setGetHistoryTime(address)
        val deviceList = BleManager.getInstance().allConnectedDevice.filter { it.mac == address }
        if (deviceList.isNotEmpty()) {
            BleManager.getInstance().write(
                deviceList[0],
                BLUETOOTH_SERVICE_UUID_960,
                BLUETOOTH_WRITE_UUID_960,
                HexUtil.hexStringToBytes(msg),
                object : BleWriteCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray
                    ) {
                        // 发送数据到设备成功
                        logD(" 蓝牙发送数据成功 getHistoryTemp  $msg")

                    }

                    override fun onWriteFailure(exception: BleException) {
                        logD(" 蓝牙发送数据失败  ${exception.toString()}")
                        // 发送数据到设备失败

                    }
                })
        }
    }

    /**
     * 获取探针状态
     */
    fun getProbeStatus(address: String, probeIndex: Int) {
        val msg = "010$probeIndex"
        println("getProbeStatus msg====$msg")
        val deviceList = BleManager.getInstance().allConnectedDevice.filter { it.mac == address }
        if (deviceList.isNotEmpty()) {
            BleManager.getInstance().write(
                deviceList[0],
                BLUETOOTH_SERVICE_UUID_960,
                BLUETOOTH_WRITE_UUID_960,
                HexUtil.hexStringToBytes(msg),
                object : BleWriteCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray
                    ) {
                        // 发送数据到设备成功
                        logD(" 蓝牙发送数据成功 getProbeStatus  $msg")

                    }

                    override fun onWriteFailure(exception: BleException) {
                        logD(" 蓝牙发送数据失败  ${exception.toString()}")
                        // 发送数据到设备失败

                    }
                })
        }
    }


    /**
     * 获取电量
     */
    fun getBoxBattery(address: String) {
        val msg = "05"
        println("getBoxBattery msg====$msg")
        val deviceList = BleManager.getInstance().allConnectedDevice.filter { it.mac == address }
        if (deviceList.isNotEmpty()) {
            BleManager.getInstance().write(
                deviceList[0],
                BLUETOOTH_SERVICE_UUID_960,
                BLUETOOTH_WRITE_UUID_960,
                HexUtil.hexStringToBytes(msg),
                object : BleWriteCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray
                    ) {
                        // 发送数据到设备成功
                        logD(" 蓝牙发送数据成功 getBoxBattery  $msg")

                    }

                    override fun onWriteFailure(exception: BleException) {
                        logD(" 蓝牙发送数据失败  ${exception.toString()}")
                        // 发送数据到设备失败

                    }
                })
        }
    }

}